import * as sqlite_opt from "@/utils/sqliteUtil.js";

const visualListTableName = "t_acc_sum";

export const requireVisualPlantData = (inMacType) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT `;
		sqlStr += `
		       plt_name 
		`
		sqlStr += `from ` + visualListTableName + ` where visible = 1`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireVisualDataByDmgCode = (inDmgCode) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += `from ` + visualListTableName + ` where visible = 1 and dmg_code = ${inDmgCode} `

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireVisualListData = (inMacType, inPltName ,isPlan) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT `;
		sqlStr += `
			*
		`
		sqlStr += `from ` + visualListTableName;
		sqlStr += ` where `;
		sqlStr += "   1 = 1 "
		sqlStr += inMacType ? (` AND mac_type = ` + inMacType) : " ";
		sqlStr += inPltName ? (` AND plt_name = '` + inPltName) + "' " : " ";
		// sqlStr += isPlan ? (` AND (plan_mac_numb = 1 or plan_mac_numb = 2 or plan_mac_numb = 3)`) : " ";
		
		sqlStr += `  AND visible = 1  ORDER BY mac_numb`

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

/**
 * 获取计划实施状态 
 * 查询的状态数组   
 * 3：双同心圆  作业中
	1：单圆   未实施
	2：实心圆 已实施
	4：未计划
	
	else if(e[i].wrk_cls_code >= 0 && e[i].progress == 2) {
		e[i].planSign = '●';
	}
	else if(e[i].wrk_cls_code >= 0 && e[i].progress == 0) {
		e[i].planSign = '○';
	}
	
	
 */
export const requireVisualImplementationData = (inMacType) => {
	console.log("requireVisualImplementationData    ==== =  = = = = =" + inMacType )
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT DISTINCT dmg_code, wrk_cls_code ,progress , mac_numb `;
		sqlStr += ` from ` + visualListTableName + ` where 1 = 1 `
		sqlStr += ` and mac_type =  ` + inMacType ;
		sqlStr += ` and wrk_cls_code != '' and wrk_cls_code != 0  `;
		 
		console.log("requireVisualImplementationData  ---------------  sql:   ")
		console.log(sqlStr)
				
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateVisualItemDataInfos = (inObj, inDmgCode) => {
	console.log("updateVisualItemDataInfos    ==== =  = = = = =" + inDmgCode )

	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item , index) => {
		valStr += item + `  =  "`;
		valStr += inObj[item] + `" ` ;
		
		if (keys.length - 1 != index)
			valStr += ","
	})
	
	return new Promise((resolve, reject) => {
		let sqlStr = `update ${visualListTableName} set ${valStr} where dmg_code = "${inDmgCode}"`;
		console.log("updateVisualItemDataInfos --- re")
		console.log(sqlStr)
		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}


export const genVisualListPlanState = (inItemData) => {
	// console.log("genVisualListPlanState  ==== =  = = = = =" )
	// console.log( `plan_kbn = ${inItemData.plan_kbn} progress = ${inItemData.progress}  wrk_cls_code = ${inItemData.wrk_cls_code}` )
	let re = "";
	if(inItemData.plan_kbn == 1) {
		re = "○";
		if(inItemData.progress == 0)
			re = "○";
		else if(inItemData.progress == 1)
			re = "◎";
		else if(inItemData.progress == 2)
			re = "●";
	} else if(inItemData.plan_kbn == 0) {
		re = "";
		if(inItemData.progress == 0) 
			re = "";
		if(inItemData.progress == 1)
			re = "◎";
		else if(inItemData.progress == 2)
			re = "●";		
	}
	return re;
}